package node;

import lexer.LexType;
import parser.Parser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ConstDeclNode extends ASTNode {
    public final String name = "<ConstDecl>";
    // ConstDecl → 'const' BType ConstDef { ',' ConstDef } ';'
    private final BTypeNode bTypeNode;
    private List<ConstDefNode> constDefNodes = new ArrayList<>();

    public ConstDeclNode(BTypeNode bTypeNode, List<ConstDefNode> constDefNodes) {
        this.bTypeNode = bTypeNode;
        this.constDefNodes = constDefNodes;
    }

    @Override
    public void show() throws IOException {
        Parser.parserWriter.write(LexType.CONSTTK + " " + "const" + "\n");
        bTypeNode.show();
        // 按照格式输出 { ',' ConstDef } ';'
        // 第一个ConstDef前没有逗号，后面的前面有逗号
        for (int i = 0; i < constDefNodes.size(); i++) {
            if (i == 0) {
                constDefNodes.get(i).show();
            } else {
                Parser.parserWriter.write(LexType.COMMA + " " + "," + "\n");
                constDefNodes.get(i).show();
            }
        }
        Parser.parserWriter.write(LexType.SEMICN + " " + ";" + "\n");
        Parser.parserWriter.write(name + "\n");
    }

    public BTypeNode getbTypeNode() {
        return bTypeNode;
    }

    public List<ConstDefNode> getConstDefNodes() {
        return constDefNodes;
    }
}
